layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;

    /**
     * 商品管理--订单管理
     */
    var Order = {
        tableId: "orderTable",    //表格id
    };


    Order.initColumn = function () {
        return [[
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'ordersn', sort: true, title: '订单编号'},
            {field: 'userId', sort: true, title: '用户ID'},
            //判断订单状态： 1待付款；2待发货;3已发货 4已完成 5申请退款中 6退款成功 7已取消
            {field: 'order_status', sort: true, title: '订单状态',templet: function (data) {
                                                                    if (data.order_status=='1'){
                                                                        return "待付款";
                                                                    }else if (data.order_status=='2'){
                                                                        return "待发货";
                                                                    }else if (data.order_status=='3'){
                                                                        return "已发货";
                                                                    }else if (data.order_status=='4'){
                                                                        return "已完成";
                                                                    }else if (data.order_status=='5'){
                                                                        return "申请退款中";
                                                                    }else if (data.order_status=='6'){
                                                                        return "退款成功";
                                                                    }else if (data.order_status=='7'){
                                                                        return "已取消";
                                                                    }
                                                                 }
            },
            {field: 'totalPrice', sort: true, title: '订单金额'},
            {field: 'payFee', sort: true, title: '支付金额'},
            {field: 'createTime', sort: true, title: '下单时间'},
            {field: 'address', sort: true, title: '收货地址',templet:'<div><span class="city"></span></div>'},
            {field: 'remark', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 280}
        ]];
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Order.tableId,
        url: Feng.ctxPath + '/ord/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Order.initColumn(),
        done: function(res){
            x(res.data);
        }

    });

    function x(d) {
        for (var i=0;i<d.length;i++){
            var a=d[i].address;
            var obj = eval("("+a+")");
            var str=obj.provinceName+","+obj.cityName+","+obj.countyName+","+obj.detailInfo;
            $(".city").eq(i).html(str);
        }};


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Order.search();
    });

    /**
     * 获取查询条件
     */
    Order.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(Order.tableId, {where: queryData});
    };

    /**
     * 弹出修改页面
     */
    Order.openModifyOrder = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改订单状态',
            content: Feng.ctxPath + '/ord/ordPageUpdate?id='+data.id,
            //结束时事件
            end: function () {
                admin.getTempData('formOk') && table.reload(Order.tableId);
            }
        });
    };

    //发货
    Order.onShipmentOrder = function (data) {
            var ajax = new $ax(Feng.ctxPath + '/ord/shipment?id='+data.id, function (data) {
                if (data.toString()=="true") {
                    Feng.success("发货成功!");
                    window.location.reload();
                }else {
                    Feng.erro("该订单状态无法发货!");
                }
            });
            ajax.set("id", data.id);
            ajax.start();
    };

    //退款
    Order.onRefundOrder = function (data) {
        var ajax = new $ax(Feng.ctxPath + '/ord/refund?id='+data.id, function (data) {
            if (data.toString()=="true") {
                Feng.success("退款成功!");
                window.location.reload();
            }else {
                Feng.error("该订单状态无法退款!");
            }
        });
        ajax.set("id", data.id);
        ajax.start();
    };

    // 工具条点击事件
    table.on('tool(' + Order.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            Order.openModifyOrder(data);
        } else if (layEvent === 'shipment') {
            Order.onShipmentOrder(data);
        } else if (layEvent === 'refund') {
            Order.onRefundOrder(data);
        }
    });
});